package com.eleven.mall.service.impl;

import com.eleven.mall.mapper.PermissionApiMapper;
import com.eleven.mall.mapper.PermissionMapper;
import com.eleven.mall.pojo.PermissionOut;
import com.eleven.mall.pojo.entity.Permission;
import com.eleven.mall.pojo.entity.PermissionApi;
import com.eleven.mall.pojo.entity.PermissionApiExample;
import com.eleven.mall.pojo.entity.PermissionExample;
import com.eleven.mall.service.PermissionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Rui_simple
 * @Date: 2022/10/26 13:15
 * @Description: PermissionServiceImpl
 * @Version 1.0.0
 */
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {
    PermissionMapper permissionMapper;
    PermissionApiMapper permissionApiMapper;

    /**
     * 查询权限列表
     * 1、查询权限列表
     * 2、根据权限列表的 pid 查询 permission_api 对应的path
     * @return
     */
    @Override
    public List<PermissionOut> getPermissionList() {
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andPsLevelNotEqualTo("0");
        List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);
        ArrayList<PermissionOut> permissionOutArrayList = new ArrayList<>();
        for (Permission permission : permissionList) {
            PermissionOut permissionOut = new PermissionOut();
            permissionOut.setId(permission.getPsId());
            permissionOut.setAuthName(permission.getPsName());
            permissionOut.setLevel(permission.getPsLevel());
            permissionOut.setPid(permission.getPsPid());
            permissionOut.setPath(getPathById(permission.getPsPid()));
            permissionOutArrayList.add(permissionOut);
        }
        return permissionOutArrayList;
    }
    //2、根据权限列表的 pid 查询 permission_api 对应的path
    private String getPathById(Short id){
        PermissionApiExample example = new PermissionApiExample();
        example.createCriteria().andPsIdEqualTo(id.intValue());
        List<PermissionApi> apiList = permissionApiMapper.selectByExample(example);
        PermissionApi api = apiList.get(0);
        return api.getPsApiPath();
    }
}
